//
//  QWImageMangerConfig.m
//  Pods
//
//  Created by ranjingfu on 16/1/13.
//
//

#import "QWImageMangerConfig.h"
#import "NSDate-Utilities.h"
#import "MBProgressHUD.h"
#define AUDIO_BIT_RATE       64000

static QWImageMangerConfig *baseConfig = nil;

@implementation QWImageMangerConfig

+ (instancetype)shareInsatnce
{
    
    if (baseConfig == nil) {
        baseConfig = [[QWImageMangerConfig alloc] init];
    }
    return baseConfig;
}

+ (NSString *)generateGuid {
    CFUUIDRef	uuidObj = CFUUIDCreate(nil);//create a new UUID
    //get the string representation of the UUID
    NSString	*uuidString = (NSString*)CFBridgingRelease(CFUUIDCreateString(nil, uuidObj));
    CFRelease(uuidObj);
    return uuidString;
}
- (id)init
{
    self = [super init];
    if (self) {
        self.mainFontBlueColor = [UIColor colorWithRed:34.0/255.0 green:188.0/255.0 blue:240.0/255.0 alpha:1];
        self.customBoldFont = [UIFont fontWithName:@"NotoSansHans-Medium" size:14];
        self.audioBitRate = AUDIO_BIT_RATE;
        self.highVideoBitRate = VIDEO_BIT_RATE_HIGH;
        self.normalVideoBitRate = VIDEO_BIT_RATE_LOW;
    }
    return self;
}


+ (ALAssetsLibrary *)sharedAssetsLibrary
{
    // structure used to test whether the block has completed or not
    static dispatch_once_t p = 0;
    
    // initialize sharedObject as nil (first call only)
    __strong static id _sharedObject = nil;
    
    // executes a block object once and only once for the lifetime of an application
    dispatch_once(&p, ^{
        _sharedObject = [[ALAssetsLibrary alloc] init];
        [ALAssetsLibrary disableSharedPhotoStreamsSupport];
    });
    
    // returns the same object each time
    return _sharedObject;
}


+(NSInteger)getLastFlashMode
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    return [[defaults objectForKey:@"LastFlashMode"] intValue];
}

+(void)setLastFlashMode:(NSInteger)flashMode
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:[NSNumber numberWithInteger:flashMode] forKey:@"LastFlashMode"];
}

+(UIImage*) getStrenchImageHasCache:(NSString*)imageName
{
    UIImage * img = [QWImageMangerConfig  imageNamed:imageName];
    UIImage * imageS = [img stretchableImageWithLeftCapWidth:img.size.width/2 topCapHeight:img.size.height/2];
    return imageS;
}

+(UIImage*)imageNamed:(NSString*)imageName
{
    
    UIImage  *image = [UIImage  imageNamed:[NSString stringWithFormat:@"%@/%@",@"QWImagePickerManager.bundle/",imageName]];
    if (image == nil)
    {
        image = [UIImage imageNamed:imageName];
    }
    return image;
}


+(NSString *) createAFolderOnDisk:(NSString *)folderName{
    NSString* document = [NSHomeDirectory() stringByAppendingFormat:CACHES_DIR];
    NSFileManager * fileManager = [[NSFileManager alloc]init];
    NSString *folderNameDirectory = [document stringByAppendingPathComponent:folderName];
    BOOL isExits = NO;
    [fileManager fileExistsAtPath:folderNameDirectory isDirectory:(&isExits)];
    if(isExits){
        //        NSLog(@"folder exits : %@",folderNameDirectory);
    }
    else{
        [fileManager createDirectoryAtPath:folderNameDirectory withIntermediateDirectories:YES attributes:nil error:nil];
        //        NSLog(@"create a new folder : %@",folderNameDirectory);
    }
    return folderNameDirectory;
}


+ (NSString *)convertDateToAlbumDateString:(NSDate *)date
{
    NSString *prefix = @"";
    NSString *formatStr = @"";
    
    
    if (date == nil) {
        return @"";
    }
    
    if ([date isToday]) {
        prefix = QWImagePickerLocalizedString(@"Today ", nil);
        formatStr = @"EEEE";
    } else if ([date isYesterday]) {
        prefix = QWImagePickerLocalizedString(@"Yesterday ", nil);
        formatStr = @"EEEE";
    } else {
        formatStr = @"yyyy-MM-dd EEEE";
    }
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:formatStr];
    [formatter setTimeZone:[NSTimeZone localTimeZone]];
    
    if ([formatter respondsToSelector:@selector(setLocale:)]) {
        NSString * deviceLanguage = [[NSLocale preferredLanguages] objectAtIndex:0];
        [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:deviceLanguage]];
    }
    
    NSString *stringFromDate = [formatter stringFromDate:date];
    
    return [prefix stringByAppendingString:stringFromDate];
}
+ (BOOL)alert:(NSString*)msg
{
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:msg
                                                        message:nil
                                                       delegate:nil
                                              cancelButtonTitle:QWImagePickerLocalizedString(@"OK", nil) otherButtonTitles:nil, nil];
    [alertView show];
    return YES;
}


+ (void) requestErorrMBProgressHUDInWindow:(NSString*)msg
{
   
    UIApplication  *app = [UIApplication sharedApplication];
    
    //    if (appDele.appMode == QWAppOfflineMode) return;
    [self requestErorrMBProgressHUDDisplay:app.keyWindow msg:msg];
}

+ (void) requestErorrMBProgressHUDDisplay:(UIView*)parentView msg:(NSString*) msg
{
    
    //    OntheRoadAppDelegate * appDele = appDelegate;
    //    if (appDele.appMode == QWAppOfflineMode) return;
    if (!parentView) {
        [self requestErorrMBProgressHUDInWindow:msg];
        return;
    }
    
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:parentView];
    [parentView addSubview:HUD];
    
    HUD.removeFromSuperViewOnHide = YES;
    
    HUD.customView = [[UIImageView alloc] initWithImage:[QWImageMangerConfig  imageNamed:@"icon_failure"]];
    
    // Set custom view mode
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.detailsLabelText = msg;
    
    [HUD show:NO];
    [HUD hide:YES afterDelay:1.1];
    //    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:parentView animated:YES];
    //    hud.customView = [[[UIImageView alloc] initWithImage:[UIImage pngImageNamedNeedReuse:@"37x-Checkmark.png"]] autorelease];
    //	hud.labelText = @"Network Error:";
    //    hud.detailsLabelText = msg;
    //    [hud show:YES];
    //    [hud hide:YES afterDelay:1];
    
}

+ (void)requestSuccessMBProgressHUDDisplay:(UIView*)parentView msg:(NSString*) msg
{
    if (!parentView) {
        [self requestErorrMBProgressHUDInWindow:msg];
        return;
    }
    
    MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:parentView];
    [parentView addSubview:HUD];
    
    HUD.removeFromSuperViewOnHide = YES;
    
    HUD.customView = [[UIImageView alloc] initWithImage:[QWImageMangerConfig  imageNamed:@"icon_succeed"]];
    
    // Set custom view mode
    HUD.mode = MBProgressHUDModeCustomView;
    HUD.labelText = msg;
    
    [HUD show:NO];
    [HUD hide:YES afterDelay:0.8];
}
+ (NSBundle*)baseBundle
{
    NSString *mainBundlePath = [[NSBundle mainBundle] executablePath];
    NSString  *strBundlePath =  [mainBundlePath stringByDeletingLastPathComponent];
    strBundlePath = [strBundlePath stringByAppendingPathComponent:@"/QWImagePickerManager.bundle"];
    NSBundle  *bundle = [NSBundle bundleWithPath:strBundlePath];
    if (bundle == nil)
    {
        return [NSBundle mainBundle];
    }
    return bundle;
}
@end
